package com.itboye.ihomebank.activity.homefragment;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.PersonDataBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.CashierInputFilter;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.WritePassPopupWindow;
import com.itboye.ihomebank.util.XImageLoader;

import java.util.Observable;
import java.util.Observer;

/**
 * 付佣金
 */
public class ActivityFuYongOne extends BaseOtherActivity implements Observer {
    View v_statusbar;
    UserPresenter userPresenter;
    ImageView head;
    TextView name, phone;
    EditText edit_money;
    Button ok;
    private PersonDataBean dataBean;
    private String headImg;
    private String money;
    private String mobile;
    public static ActivityFuYongOne context;
    String contractNo;
    String leixing;
    TextView add_shap_title_tv;
    private WritePassPopupWindow mWritePass;
    EditText pass;

    private String uid;

    public static ActivityFuYongOne getContext() {
        return context;
    }

    public static void setContext(ActivityFuYongOne context) {
        ActivityFuYongOne.context = context;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.ok:
                money = edit_money.getText().toString();
                if (money.equals("")) {
                    ByAlert.alert("请输入转账金额");
                    return;
                }
                if(leixing.equals("1") ){//经纪人支付的时候不需要输入支付密码
                    showProgressDialog("正在支付中,请稍后",false);
                    userPresenter.zhuanzhang(uid, mobile, Double.parseDouble(money) *100+"", contractNo,
                            "1");
                }else{
                    mWritePass = new WritePassPopupWindow(this,
                            onclickListener);
                    pass = (EditText) mWritePass.getContentView()
                            .findViewById(R.id.pass);
                    mWritePass.setBackgroundDrawable(new BitmapDrawable());
                    mWritePass.setFocusable(true);
                    mWritePass.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
                    mWritePass.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
                    mWritePass.showAtLocation(v, Gravity.BOTTOM, 0, 0);
                }

                break;
            case R.id.close_icon:
                finish();
                break;
            default:
                break;
        }
    }



    View.OnClickListener onclickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.btn_ok) {
                String password = pass.getText().toString();
                if (password.equals("")) {
                    ByAlert.alert("请输入支付密码");
                    return;
                }

                userPresenter.ZhiFuPassYanZheng(uid, password);
//                showProgressDialog("正在验证中,请稍后", false);

//				Intent intent = new Intent(ActivityFuYongOne.this, FuYongTwo.class);
//				intent.putExtra("money", money);
//				intent.putExtra("phone", mobile);
//				intent.putExtra("contractNo", contractNo==null?"":contractNo);
//				startActivity(intent);
            } else {

            }
            mWritePass.dismiss();
        }
    };

    @Override
    protected int layout_id() {
        // TODO Auto-generated method stub
        return R.layout.activity_fuyongone;
    }

    @Override
    protected void onInitController() {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onInit() {
        // TODO Auto-generated method stub
        setStatusBarHeight(v_statusbar);
        context = this;
        contractNo = getIntent().getStringExtra("contractNo");
        leixing = getIntent().getStringExtra("leixing");
        if (leixing.equals("1")) {//经纪人传过来的
            add_shap_title_tv.setText("转账到经纪人用户");
        } else {//房东身份
            add_shap_title_tv.setText("当前账户为房东");
        }
        money = getIntent().getStringExtra("money");
        userPresenter = new UserPresenter(this);
        mobile = getIntent().getStringExtra("phone");
        userPresenter.queryUserByPhone(mobile);
        uid = SPUtils.get(ActivityFuYongOne.this, null, SPContants.USER_ID, "") + "";
        InputFilter[] filter = { new CashierInputFilter() };
        edit_money.setFilters(filter);// 设置输入位金额
    }

    @Override
    protected void onReadIntent(Bundle bundle) {
        // TODO Auto-generated method stub

    }

    @Override
    public void update(Observable observable, Object data) {
        // TODO Auto-generated method stub
        ResultEntity entity = handlerError(data);
        try {
            closeProgressDialog();
        }catch (Exception e){

        }
        if (entity != null) {
            if (entity.getEventType() == UserPresenter.queryUserByPhone_success) {
                dataBean = (PersonDataBean) entity.getData();
                if (dataBean != null) {
                    headImg = dataBean.getHead();
                    XImageLoader.load(headImg, head);
                    mobile = dataBean.getMobile();
                    phone.setText(dataBean.getMobile());
                    name.setText(dataBean.getRealname());
               //     edit_money.setText(Double.parseDouble(money) / 100 + "");
                }
            } else if (entity.getEventType() == UserPresenter.queryUserByPhone_success) {
                ByAlert.alert(entity.getData());
            } else if (entity.getEventType() == UserPresenter.zhiFuPassYanZheng_success) {
  //              ByAlert.alert(entity.getData());
//                try {
//                    closeProgressDialog();
//                }catch (Exception e){
//
//                }

                showProgressDialog("正在支付中,请稍后",false);

                userPresenter.zhuanzhang(uid, mobile, Double.parseDouble(money) *100+"", contractNo,
                        "1");
            } else if (entity.getEventType() == UserPresenter.zhiFuPassYanZheng_fail) {
                ByAlert.alert(entity.getData());
                try {

                    closeProgressDialog();
                }catch (Exception e){

                }
            }if (entity.getEventType() == UserPresenter.zhuanzhang_success) {
                ByAlert.alert(entity.getData());
                try {
                    closeProgressDialog();
                }catch (Exception e){

                }
                finish();
            } else if (entity.getEventType() == UserPresenter.zhuanzhang_fail) {
                ByAlert.alert(entity.getData());
                try {
                    closeProgressDialog();
                }catch (Exception e){

                }
            }
        }
    }

}
